﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace uTorrentRemote.TokenSources
{
    public class SingleFileTokenSource : ITokenSource, IDisposable
    {
        private string filename = null;
        private StreamReader rdr = null;
        private ILog log = null;
        
        public SingleFileTokenSource(string filename, ILog log)
        {
            this.filename = filename;
            try
            {
                this.rdr = new StreamReader(File.Open(filename, FileMode.Open, FileAccess.Read));
            }
            catch (Exception ex)
            {
                log.Write("Warning: " + ex.Message);
            }
        }
        

        public bool IsNextTokenAvailable()
        {
            if (rdr != null)
            {
                return !rdr.EndOfStream;
            }
            return false;
        }

        public Token FetchNextToken()
        {
            Token token = new Token(rdr.ReadLine());
            return token;
        }

        public void Dispose()
        {
            rdr.Dispose();
        }
    }
}
